$(function () {
    //begin列表信息
    $("#employee_datagrid").datagrid({
        url: "/employee/list",
        onClickRow: function (index, row) {
            if (row.state == 0) {
                $("#remove").linkbutton({text: "离职"});
            } else {
                $("#remove").linkbutton({text: "复职"});
            }
        },
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100, hidden: true},
            {field: "username", title: "用户名", width: 100},
            {field: "jobNumber", title: "工号", width: 100},
            {
                field: "department", title: "部门", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                } else {
                    return "未分配";
                }
            }
            },
            {field: "positions", title: "职位", width: 100},
            {field: "hireType", title: "招聘方式", width: 100},
            {field: "recoed", title: "学历", width: 100},
            {field: "jobs", title: "岗位", width: 100},
            {field: "gender", title: "性别", width: 100},
            {field: "age", title: "年龄", width: 100},
            {field: "tel", title: "电话", width: 100},
            {field: "email", title: "邮箱", width: 100},
            {field: "birthTime", title: "出生日期", width: 100},
            {field: "inputTime", title: "入职日期", width: 100},
            {field: "serviceLength", title: "工龄", width: 100},
            {
                field: "state", title: "状态", width: 100, formatter: function (value, row, index) {
                if (value == 0) {
                    return "<font color='green'>在职</font>";
                } else {
                    return "<font color='red'>离职</font>";
                }
            }
            },
            {field: "marital", title: "是否结婚", width: 100},
            {field: "address", title: "地址", width: 100}
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber: 1,
        pageSize: 3,
        pageList: [3, 5, 10, 20],
        toolbar: "#employee_tb"
    });


    //begin添加/编辑对话框
    $("#employee_dialog").dialog({
        width: 700,
        height: 500,
        buttons: "#employee_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin处理功能方法的对象
    var obj = {
            //begin添加按钮
            add: function () {
                $("#employee_form").form("clear");
                $("#salary_form").form("clear");
                $("#employee_dialog").dialog("setTitle", "员工添加");
                $('#tt').tabs('getTab', "工资信息").panel('options').tab.hide();
                $('#tt').tabs('getTab', "教育情况").panel('options').tab.hide();
                $('#tt').tabs('getTab', "家庭情况").panel('options').tab.hide();
                $('#tt').tabs('getTab', "工作经历").panel('options').tab.hide();
                $('#tt').tabs('getTab', "身份证上传").panel('options').tab.hide();
                $("#tt").tabs("select", 0);
                /* var tabs=$("#tt").tabs("tabs");
                 for(var i=0;i<tabs.length;i++){
                 if(i>0){
                 tabs[i].hide(1000);
                 }
                 }*/
                $("#tt").tabs({'select': 0});
                $("#salary_tb").tabs({selected: 'true'});
                $("#employee_dialog").dialog("open");
            },
            //end添加按钮

            //begin编辑按钮
            edit: function () {
                $("#employee_form").form("clear");
                $("#salary_form").form("clear");
                $('#tt').tabs('getTab', "工资信息").panel('options').tab.show();
                $('#tt').tabs('getTab', "教育情况").panel('options').tab.show();
                $('#tt').tabs('getTab', "家庭情况").panel('options').tab.show();
                $('#tt').tabs('getTab', "工作经历").panel('options').tab.show();
                $('#tt').tabs('getTab', "身份证上传").panel('options').tab.show();
                $("#employee_dialog").dialog("setTitle", "员工编辑");
                var row = $("#employee_datagrid").datagrid("getSelected");

                //点击编辑的时候,将图片和员工的隐藏域进行值填充,用作更新身份证照片的信息
                $("#imageEmployeeId").val(row.id);
                $("#hiddenImagePath").val(row.imagepath);
                $("#showImage").prop("src", row.realPath);

                var employeeId = row.id;
                $.get("/employee/selectRoleByEmployeeId", {employeeId: employeeId}, function (data) {
                    $("#role").combobox("setValues", data);

                });
                if (!row) {
                    $.messager.alert('温馨提示', '请选择要操作的行!');
                    return;
                }
                //begin教育情况
                $("#homeId").datagrid({
                    iconCls: 'icon-edit',
                    onClickRow: function (index, row) {
                        if (homeIndex != index) {
                            if (endEditing()) {
                                //选择选中的这一行开始编辑
                                $("#homeId").datagrid("selectRow", index).datagrid("beginEdit", index);
                                //吧这个的index赋值给editIndex
                                homeIndex = index;
                            } else {
                                $("#homeId").datagrid("selectRow", homeIndex);
                            }
                        }
                    },
                    singleSelect: true,
                    url: '/home/selectByEmployeeId?employeeId=' + employeeId,
                    toolbar: '#home_tb',
                    method: 'get',
                    columns: [[
                        {field: "id", title: "编号", hidden: true},
                        {
                            field: "title", title: "称谓", width: 100,
                            editor: {
                                type: 'combobox',
                                options: {
                                    valueField: 'name',
                                    panelHeight: 'auto',
                                    textField: 'name',
                                    method: 'get',
                                    url: '/dataDictionaryItem/selectByParentId?id=27',
                                    required: true,
                                    on: '', off: '姐姐'
                                }
                            }
                        },
                        {field: "name", title: "姓名", editor: 'textbox', width: 100},
                        {field: "company", title: "工作单位", editor: 'textbox', width: 120},
                        {field: "job", title: "岗位", editor: 'textbox', width: 120},
                        {field: "birthdate", editor: 'datebox', title: "出生日期", width: 120},
                        {field: "note", editor: 'textbox', title: "备注", width: 120}
                    ]]
                });
                //end教育情况
                //begin教育情况
                $("#educationId").datagrid({
                    iconCls: 'icon-edit',
                    singleSelect: true,
                    toolbar: '#education_tb',
                    url: '/education/selectByEmployeeId?employeeId=' + employeeId,
                    method: 'get',
                    onClickRow: function (index, row) {
                        if (editIndex != index) {
                            if (endEditing()) {
                                //选择选中的这一行开始编辑
                                $("#educationId").datagrid("selectRow", index).datagrid("beginEdit", index);
                                //吧这个的index赋值给editIndex
                                editIndex = index;
                            } else {
                                $("#educationId").datagrid("selectRow", editIndex);
                            }
                        }
                    },
                    columns: [[
                        {field: "id", title: "编号", hidden: true},
                        {
                            field: "school", title: "毕业学校", width: 100,
                            editor: {
                                type: 'combobox',
                                options: {
                                    valueField: 'name',
                                    panelHeight: 'auto',
                                    textField: 'name',
                                    method: 'get',
                                    url: '/dataDictionaryItem/selectByParentId?id=28',
                                    required: true,
                                    on: '', off: '北大'
                                }
                            }
                        },
                        {field: "major", editor: 'textbox', title: "专业", width: 100},
                        {field: "certifier", editor: 'textbox', title: "证明人", width: 120},
                        {field: "begindate", editor: 'datebox', title: "入学时间", width: 120},
                        {field: "enddate", editor: 'datebox', title: "毕业时间", width: 120},
                        {field: "note", editor: 'textbox', title: "备注", width: 120}
                    ]]
                });
                //end教育情况
                //begin教育情况
                $("#workId").datagrid({
                    iconCls: 'icon-edit',
                    singleSelect: true,
                    toolbar: '#work_tb',
                    method: 'get',
                    onClickRow: function (index, row) {
                        if (workIndex != index) {
                            if (endEditing()) {
                                //选择选中的这一行开始编辑
                                $("#workId").datagrid("selectRow", index).datagrid("beginEdit", index);
                                //吧这个的index赋值给editIndex
                                workIndex = index;
                            } else {
                                $("#workId").datagrid("selectRow", workIndex);
                            }
                        }
                    },
                    url: '/work/selectByEmployeeId?employeeId=' + employeeId,
                    columns: [[
                        {
                            field: "id", title: "编号", hidden: true
                        },
                        {field: "company", editor: 'textbox', title: "工作单位", width: 100},
                        {
                            field: "occupation", title: "职业", width: 100,
                            editor: {
                                type: 'combobox',
                                options: {
                                    valueField: 'name',
                                    panelHeight: 'auto',
                                    textField: 'name',
                                    method: 'get',
                                    url: '/dataDictionaryItem/selectByParentId?id=3',
                                    required: true
                                }
                            }
                        },
                        {
                            field: "job", title: "岗位", width: 120,
                            editor: {
                                type: 'combobox',
                                options: {
                                    valueField: 'name',
                                    panelHeight: 'auto',
                                    textField: 'name',
                                    method: 'get',
                                    url: '/dataDictionaryItem/selectByParentId?id=5',
                                    required: true,
                                    on: '', off: '老板'
                                }
                            }
                        },
                        {field: "begindate", editor: 'datebox', title: "入职时间", width: 120},
                        {field: "enddate", editor: 'datebox', title: "离职时间", width: 120}
                    ]]
                });
                var employeeId = row.id;
                $.get("/salaryTemplate/selectByEmployeeId", {employeeId: employeeId}, function (data) {

                    if (data) {
                        data["employee.id"] = data.employee.id;
                    };
                    $("#salary_form").form("load", data);
                })
                //end教育情况
                if (row.department) {
                    row["department.id"] = row.department.id;
                }

                $("#employee_form").form("load", row);
                $("#employee_dialog").dialog("open");
            },
            //end编辑按钮

            //begin取消按钮
            cancel: function () {
                $("#employee_dialog").dialog("close");
            },
            //end取消按钮


            // begin 高级查询按钮
            query: function () {
                var queryname = $("#queryname").datebox("getValue");
                var position = $("#position").datebox("getValue");
                var hiretype = $("#hiretype").datebox("getValue");
                $("#employee_datagrid").datagrid("load", {
                    queryname: queryname,
                    position: position,
                    hiretype: hiretype
                })
            },
            // end 高级查询
            //begin保存按钮
            save: function () {

                var employeeId = $("#employeeId").val();

                var url;
                if (employeeId) {
                    url = "/employee/update";
                    $("#salary_form").form("submit", {
                        url: "/salaryTemplate/update"
                    });
                    $("#employee_form").form("submit", {
                        url: url,
                        onSubmit: function (param) {
                            /* var pic=$("#pic").filebox("getValue");
                             param["pic"]=pic;*/
                            //先获取到role这个框里面所有的值,在遍历出来形成roles[index]
                            var role = $("#role").combobox("getValues");
                            for (var i = 0; i < role.length; i++) {
                                param["roles[" + i + "].id"] = role[i];
                            }
                            $('#educationId').datagrid('acceptChanges');
                            //先获取到role这个框里面所有的值,在遍历出来形成roles[index]
                            var rows = $("#educationId").datagrid("getRows");
                            for (var i = 0; i < rows.length; i++) {
                                param["educations[" + i + "].school"] = rows[i].school;
                                param["educations[" + i + "].major"] = rows[i].major;
                                param["educations[" + i + "].certifier"] = rows[i].certifier;
                                param["educations[" + i + "].begindate"] = rows[i].begindate;
                                param["educations[" + i + "].enddate"] = rows[i].enddate;
                                param["educations[" + i + "].note"] = rows[i].note;
                            }
                            $('#homeId').datagrid('acceptChanges');
                            //先获取到role这个框里面所有的值,在遍历出来形成roles[index]
                            var homes = $("#homeId").datagrid("getRows");
                            for (var i = 0; i < homes.length; i++) {
                                //console.log(rows[i]);
                                param["homes[" + i + "].title"] = homes[i].title;
                                param["homes[" + i + "].name"] = homes[i].name;
                                param["homes[" + i + "].company"] = homes[i].company;
                                param["homes[" + i + "].job"] = homes[i].job;
                                param["homes[" + i + "].birthdate"] = homes[i].birthdate;
                                param["homes[" + i + "].note"] = homes[i].note;
                            }
                            $('#workId').datagrid('acceptChanges');
                            var works = $("#workId").datagrid("getRows");
                            for (var i = 0; i < works.length; i++) {
                                param["works[" + i + "].company"] = works[i].company;
                                param["works[" + i + "].occupation"] = works[i].occupation;
                                param["works[" + i + "].job"] = works[i].job;
                                param["works[" + i + "].begindate"] = works[i].begindate;
                                param["works[" + i + "].enddate"] = works[i].enddate;
                            }
                        },
                        success: function (data) {
                            var data = $.parseJSON(data);
                            if (!data.success) {
                                $.messager.alert('温馨提示', data.msg);
                            } else {
                                $.messager.alert('温馨提示', "操作成功!");
                                $("#employee_dialog").dialog("close");
                                $("#employee_datagrid").datagrid("reload");
                            }
                        }
                    });

                } else {
                    url = "/employee/save";
                    $("#employee_form").form("submit", {
                            url: url,
                            onSubmit: function (param) {
                                var role = $("#role").combobox("getValues");
                                for (var i = 0; i < role.length; i++) {
                                    param["roles[" + i + "].id"] = role[i];
                                }
                            },

                            success: function (data) {
                                console.log(data);
                                var x = $.parseJSON(data);
                                console.log(x);
                                if (!x.success) {
                                    $.messager.alert('温馨提示', x.msg);
                                } else {
                                    $.messager.alert('温馨提示', "操作成功!");
                                    $("#employee_dialog").dialog("close");
                                    $("#employee_datagrid").datagrid("reload");
                                }
                            }


                        }
                    );
                }
            },
            //end保存按钮

            //begin删除按钮
            remove: function () {
                var row = $("#employee_datagrid").datagrid("getSelected");
                if (!row) {
                    $.messager.alert('温馨提示', '请选择要操作的行!');
                    return;
                }
                $.messager.confirm('确认', '您确认要操作此行吗？', function (r) {
                    if (r) {
                        //注意这里的id要从当前选中的行中获取
                        var employeeId = row.id;
                        $.get("/employee/updateState", {employeeId: employeeId}, function (data) {
                            if (!data.success) {
                                $.messager.alert('温馨提示', data.msg);
                            } else {
                                $.messager.alert('温馨提示', '操作成功!');
                                $("#employee_datagrid").datagrid("reload");
                            }
                        });
                    }
                });

            }
            ,
            //end删除按钮

            //begin刷新按钮
            reload: function () {
                $("#employee_datagrid").datagrid("reload")
            }
            ,
            //end刷新按钮
            //class="btn" data-param=""
            education_add: function () {
                if (endEditing()) {
                    //如果之前没有选中任何列,就直接添加

                    $("#educationId").datagrid('appendRow', {school: ''});
                    //获取最后一个行的index
                    editIndex = $("#educationId").datagrid('getRows').length - 1;
                    //从最后一行开始编辑
                    $("#educationId").datagrid('selectRow', editIndex)
                        .datagrid('beginEdit', editIndex);
                }
            }
            ,
            education_remove: function () {
                if (editIndex == undefined) {
                    return
                }
                $('#educationId').datagrid('cancelEdit', editIndex)
                    .datagrid('deleteRow', editIndex);
                editIndex = undefined;
            }
            ,


            education_reject: function () {
                $('#educationId').datagrid('rejectChanges');
                editIndex = undefined;
            }
            ,

            home_add: function () {
                if (enhomeIndex()) {
                    //如果之前没有选中任何列,就直接添加

                    $("#homeId").datagrid('appendRow', {title: ''});
                    //获取最后一个行的index
                    editIndex = $("#homeId").datagrid('getRows').length - 1;
                    //从最后一行开始编辑
                    $("#homeId").datagrid('selectRow', homeIndex)
                        .datagrid('beginEdit', homeIndex);
                }
            }
            ,
            home_remove: function () {
                if (homeIndex == undefined) {
                    return
                }
                $('#homeId').datagrid('cancelEdit', homeIndex)
                    .datagrid('deleteRow', homeIndex);
                homeIndex = undefined;
            }
            ,


            home_reject: function () {
                $('#homeId').datagrid('rejectChanges');
                homeIndex = undefined;
            }
            ,
            work_add: function () {
                if (enworkIndex()) {
                    //如果之前没有选中任何列,就直接添加

                    $("#workId").datagrid('appendRow', {job: ''});
                    //获取最后一个行的index
                    workIndex = $("#workId").datagrid('getRows').length - 1;
                    //从最后一行开始编辑
                    $("#workId").datagrid('selectRow', workIndex)
                        .datagrid('beginEdit', workIndex);
                }
            }
            ,
            work_remove: function () {
                if (workIndex == undefined) {
                    return
                }
                $('#workId').datagrid('cancelEdit', workIndex)
                    .datagrid('deleteRow', workIndex);
                workIndex = undefined;
            }
            ,


            work_reject: function () {
                $('#workId').datagrid('rejectChanges');
                workIndex = undefined;
            }


        }
    ;
//end处理功能方法的对象

//begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
//end处理功能方法对象,实际调用方法


})
;

var editIndex = null;
function endEditing() {
    if (editIndex == null) {
        return true;
    }
    //验证这一行,有效时返回true
    if ($("#educationId").datagrid('validateRow', editIndex)) {
        //结束编辑这一节点
        $("#educationId").datagrid('endEdit', editIndex);
        editIndex = null;
        return true;
    } else {
        return false;
    }
};

var homeIndex = null;
function enhomeIndex() {
    if (homeIndex == null) {
        return true;
    }
    //验证这一行,有效时返回true
    if ($("#homeId").datagrid('validateRow', homeIndex)) {
        //结束编辑这一节点
        $("#homeId").datagrid('endEdit', homeIndex);
        homeIndex = null;
        return true;
    } else {
        return false;
    }
};
var workIndex = null;
function enworkIndex() {
    if (workIndex == null) {
        return true;
    }
    //验证这一行,有效时返回true
    if ($("#workId").datagrid('validateRow', workIndex)) {
        //结束编辑这一节点
        $("#workId").datagrid('endEdit', workIndex);
        workIndex = null;
        return true;
    } else {
        return false;
    }
};

$(function () {
    $("#save").click(function () {
        $("#pic_form").form("submit", {
            url: "/employee/uploadImage"
        });
    });
});








